package com.imodia.checkoo.tag;

import java.io.Writer;

import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.beans.BeanUtils;
import com.imodia.checkoo.bean.CmsManagerMenuBean;
import com.imodia.checkoo.service.impl.CmsManagerMenuServiceImpl;

/**
 *
 * @since 2001-12-18
 * @author lijun
 * @version 1.0
 * @Copyright ? 2009 LinkCm Co. Ltd. All right reserved.
 */

public class PopedomTag extends RequestContextAwareTag{
	
	private int parentId;
	private String html;
	private int menuId;
	private String field;
	

	public int getMenuId() {
		return menuId;
	}
	public void setMenuId(int menuId) {
		this.menuId = menuId;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public String getHtml() {
		return html;
	}
	public void setHtml(String html) {
		this.html = html;
	}
	public String getField() {
		return field;
	}
	public void setField(String field) {
		this.field = field;
	}
	@Override
	protected int doStartTagInternal() throws Exception {
		ApplicationContext ctx = getRequestContext().getWebApplicationContext();
		CmsManagerMenuServiceImpl cmsManagerMenuService = (CmsManagerMenuServiceImpl)ctx.getBean("cmsManagerMenuService");
		CmsManagerMenuBean menagerMenu = cmsManagerMenuService.queryNavigateMenu(parentId,menuId);
		if(menagerMenu==null){
			return 0;
		}
		String[] fieldAr = field.split(",");
		int j = 0;
		
		for(String field : fieldAr){
			Object o = BeanUtils.get(menagerMenu, field);
			html = html.replaceAll("\\{"+j+"\\}", String.valueOf( o));
			j++;
		}
		Writer out = pageContext.getOut();
		out.write(html);
		out.flush();
		return 0;
	}

}
